package com.fh.shop.admin.util;


import redis.clients.jedis.Jedis;

public class RedisUtil {
    //redis赋值方法
    public static void set(String key,String value){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //redis赋值并设置过期时间
    public static void setex(String key,String value,int seconds){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.setex(key,seconds,value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //清除redisi
    public static void del(String key){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.del(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //redis取值方法
    public static String get(String key){
        Jedis connection = null;
        String redis = "";
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            return connection.get(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
        return redis;
    }


}
